package com.yuyou.fn.analyze.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.plugins.Page;
import com.yuyou.fn.analyze.entity.ClassTarget;
import com.yuyou.fn.analyze.mapper.ClassTargetMapper;
import com.yuyou.fn.analyze.service.IClassTargetService;
import com.yuyou.fn.analyze.vo.ClassTargetSaveVo;
import com.yuyou.fn.analyze.vo.ClassTargetVo;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.CourseClass;
import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.vo.CourseClassQueryVo;
import com.yuyou.fn.platform.permission.DataPermission;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * ClassTarget 表数据服务层接口实现类
 *
 */
@Service
public class ClassTargetServiceImpl extends SuperServiceImpl<ClassTargetMapper, ClassTarget,Long> implements IClassTargetService {

    @Resource
    private ICourseClassService courseClassService;

    @Override
    public Page<ClassTargetVo> findPage(CourseClassQueryVo courseClassQueryVo, DataPermission dataPermission, int pageNo, int pageSize) {
        Page<CourseClass> page=courseClassService.findCourseClassByQueryForPage(courseClassQueryVo,dataPermission,pageNo,pageSize);
        Page<ClassTargetVo> resultPage= Utils.newPage(page);

        List<CourseClass> courseClassList=page.getRecords();
        List<Long> classIdList= BeanUtils.toList(courseClassList,"classId");
        List<ClassTarget> classTargetList=this.findByIds(classIdList);
        List<Long> checkList=BeanUtils.toList(classTargetList,"classId");
        classIdList.removeAll(checkList);
        if (classIdList.size()>0){
            List<ClassTarget> saveList=new ArrayList<ClassTarget>(classIdList.size());
            for (Long classId:classIdList){
                ClassTarget classTarget=new ClassTarget();
                classTarget.setClassId(classId);
                classTarget.setCreateTime(TimeUtils.now());
                classTarget.setUpdateTime(TimeUtils.now());
                saveList.add(classTarget);
            }
            this.insertBatch(saveList);
            classTargetList.addAll(saveList);
        }
        Map<Long,ClassTarget> map=BeanUtils.toMap(classTargetList,"classId");
        if (courseClassList!=null){
            List<ClassTargetVo> classTargetVoList=new ArrayList<ClassTargetVo>(courseClassList.size());
            for (CourseClass courseClass:courseClassList){
                ClassTargetVo classTargetVo=new ClassTargetVo();
                BeanUtils.copyProperties(classTargetVo,courseClass);
                ClassTarget classTarget=map.get(courseClass.getClassId());
                if (classTarget!=null){
                    classTargetVo.setSequentialTargetRate(classTarget.getSequentialTargetRate());
                    classTargetVo.setStepTargetRate(classTarget.getStepTargetRate());
                }
                classTargetVoList.add(classTargetVo);
            }
            resultPage.setRecords(classTargetVoList);
        }
        return resultPage;
    }

    @Override
    public void updateClassTarget(ClassTargetSaveVo classTargetSaveVo) {
        if (classTargetSaveVo.getClassId()==null){
            throw new BusException("classId必传");
        }
        ClassTarget classTarget=new ClassTarget();
        BeanUtils.copyProperties(classTarget,classTargetSaveVo);
        classTarget.setUpdateTime(TimeUtils.now());
        this.updateSelectiveById(classTarget);
    }
}